<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <title>用户发布商品页面</title>
</head>
<?php 
include_once("inc.php");
include_once("base.php");
$tb_name = "goods";
// echo($_REQUEST["id"]);
if ($_REQUEST["id"]) {
	$rs = db_get_row("select * from $tb_name where id=". $_REQUEST["id"]);
}
if ($_POST){
	$data = array();
	$data["title"] = "'".$_POST["title"]."'";
	$data["categoryid"] = "'".$_POST["categoryid"]."'";
	$data["amount"] = "'".$_POST["amount"]."'";
	$data["mprice"] = "'".$_POST["mprice"]."'";
	$data["sprice"] = "'".$_POST["sprice"]."'";
	$data["userid"] = "'".$_SESSION["id"]."'";
	$data["content"] = "'".$_POST["content"]."'";
	$data["status"] = "'未审核'";
	$data["pid"]=1;
	// if($_POST["pid"]){$data["pid"] = "'".$_POST["pid"]."'";}
	if($_POST["mark1"]){$data["mark1"] = "'".$_POST["mark1"]."'";}
	if(!empty($_FILES['img']['name'])){	// !empty的功能和isset()功能一致
		$file = $_FILES['img'];//得到传输的数据
		// var_dump($file);
		// break;
		//得到文件名称
		$name = $file['name'];
		$type = strtolower(substr($name,strrpos($name,'.')+1)); //得到文件类型，并且都转化成小写
		$allow_type = array('jpg','jpeg','gif','png'); //定义允许上传的类型
		//判断文件类型是否被允许上传
		if(!in_array($type, $allow_type)){
			//如果不被允许，则直接停止程序运行
		}
		//判断是否是通过HTTP POST上传的
		$upload_path = ROOT_PATH.'/Public/Upload/'; //上传文件的存放路径
		//开始移动文件到相应的文件夹
		$mu=mt_rand(1,10000000);
		if(move_uploaded_file($file['tmp_name'],$upload_path.$mu.".".$type)){
			$fileName =$mu.".".$type;
		}else{
			//echo "Failed!";
		}
		$data["img"] = "'".$fileName."'";	
	}else{
		echo('<script type="text/javascript"> 
		alert("商品图片未上传!");
		window.history.back(-1);
		</script>');
		break;
	}
	if ($_REQUEST["id"]) {
		db_mdf($tb_name,$data,$_REQUEST["id"]);
	} else {
		db_add($tb_name,$data);
	}
	urlMsg("提交成功", $tb_name."_list.php?pid=".$_REQUEST["pid"]);
	die;
}
?>
<script>
	function checkadd()
	{
	if (document.form1.title.value=='')
	{
	alert('商品名称不能为空');
	document.form1.title.focus;
	return false
	}
	if (document.form1.mark1.value=='')
	{
	alert('新旧不能为空');
	document.form1.mark1.focus;
	return false
	}
	if (document.form1.mprice.value=='')
	{
	alert('原价不能为空');
	document.form1.mprice.focus;
	return false
	}
	if (document.form1.amount.value=='')
	{
	alert('商品数量不能为空');
	document.form1.amount.focus;
	return false
	}
	var data = document.form1.content.value;
	str = trim(data);//去除空格的
		if(data.length==0){
		alert("内容不能为空!");
		return false;
		}
	function trim(str){       
	str = str.replace(/^(\s|\u00A0)+/,'');      
	for(var i=str.length-1; i>=0; i--){           
		 if(/\S/.test(str.charAt(i))){              
			 str = str.substring(0, i+1);              
			 break;           
			 }      
		  }      
	  return str;  
	  }
	}
</script>
<body>
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td width="17" rowspan="2" valign="top" bgcolor="#FFFFFF"></td>
			<td valign="top">
				<table width="100%" height="31" border="0" cellpadding="0" cellspacing="0">
					<tr bgcolor="#FFFFFF">
						<td height="31">
							<div class="title">添加/修改商品</div>
						</td>
					</tr>
				</table>
			</td>
			<td width="16" rowspan="2" bgcolor="#FFFFFF"></td>
		</tr>
		<tr>
			<td valign="top" bgcolor="#F7F8F9">
				<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
					<tr>
						<td colspan="4" height="10"></td>
					</tr>
					<tr>
						<td width="1%">&nbsp;</td>
						<td width="96%">
							<table width="100%">
								<tr>
									<td colspan="2">
										<form name="form1" method="post" action="?" onSubmit="return checkadd()" enctype="multipart/form-data">
											<input type="hidden" name="id" value="<?php echo $rs["id"];?>" />
											<input type="hidden" name="pnumber1" value="<?php echo $rs["pnumber"];?>" />
											<input type="hidden" name="pid" value="<?php echo $_REQUEST["pid"];?>" />
											<table width="100%" class="cont">
												<tr>
													<td width="120" align="right">
														<span class="red">*</span> 商品名称：
													</td>
													<td>
														<input name="title" type="text" class="text" style="width:350px;" value="<?php echo $rs["title"];?>">
													</td>
												</tr>
												<?php 
												$categoryA = db_get_all("select * from category where pid=1 order by id asc");
												// var_dump($categoryA);
												if(!empty($categoryA[0])){
													?>
													<tr>
														<td align="right">
															<span class="red">*</span> 分类：
														</td>
														<td>
															<select name="categoryid">
																<?php
																foreach($categoryA as $row) {
																	?>
																	<option value="<?php echo $row["id"];?>" <?php if($rs["categoryid"]==$row["id"]){echo ' selected="selected" ';}?>><?php echo $row["title"];?></option>
																<?php } ?>
															</select>
														</td>
													</tr>
												<?php }?>
												<tr>
													<td align="right">
														<span class="red">*</span> 新旧程度：
													</td>
													<td>
														<input name="mark1" type="text" class="text" size="30"  maxlength="20" value="<?php echo $rs["mark1"];?>">
													</td>
												</tr>
												<tr>
													<td align="right">
														<span class="red">*</span> 原价：
													</td>
													<td>
														<input name="mprice" type="text" class="text" size="30"  maxlength="20" value="<?php echo $rs["mprice"];?>">
													</td>
												</tr>
												<tr>
													<td align="right">
														<span class="red">*</span> 现价：
													</td>
													<td>
														<input name="sprice" type="text" class="text" size="30"  maxlength="20" value="<?php echo $rs["sprice"];?>">
													</td>
												</tr>
												<tr>
													<td align="right">
														<span class="red">*</span> 商品数量：
													</td>
													<td>
														<input name="amount" type="text" class="text" size="30"  maxlength="20" value="<?php echo $rs["amount"];?>">
													</td>
												</tr>
												<tr>
													<td align="right">商品图片：</td>
													<td>
														<input type="file" name="img" class="text" id="img"><?php if(!empty($rs['img'])){?><img src="../Public/Upload/<?php echo $rs["img"];?>" height="50" width="50"/><?php }?>
													</td>
												</tr>
												<tr>
													<td align="right">商品介绍：</td>
													<td>
														<script src="../admin/kindeditor/kindeditor-all-min.js"></script>
														<script src="../admin/kindeditor/lang/zh-CN.js"></script>
														<script>
														    //编辑器
														    var KE;
														    KindEditor.ready(function (K) {
														        KE = K.create('#Contents', {
														            allowFileManager: true, //浏览图片空间
														            filterMode: false, //HTML特殊代码过滤
														            afterBlur: function () { this.sync(); }, //编辑器失去焦点(blur)时执行的回调函数（将编辑器的HTML数据同步到textarea）
														            afterUpload: function (url, data, name) { //上传文件后执行的回调函数，必须为3个参数
														                if (name == "image" || name == "multiimage") { //单个和批量上传图片时
														                    var img = new Image(); img.src = url;
														                    img.onload = function () { //图片必须加载完成才能获取尺寸
														                        if (img.width > 100) {
														                            KE.html(KE.html().replace('<img src="' + url + '" width="100" height="100"/>', '<img src="' + url + '" width="100" height="100px"/>'))
														                            //KE.html(KE.html().replace('<img src="' + url + '" width="300"/>', '<img src="' + url + '" width="300"/>'))
														                        }
														 
														                    }
														                }
														            }
														        });
														        KindEditor.create('#Contents', { allowImageUpload: false, resizeType: 1, width: "600px", height: "300px" });
														    });
														</script>
														<!-- <script id="editor" type="text/plain" style="width:100%;height:200px;"><?php echo $rs['content'];?></script> -->
														<textarea name="content" id="Contents" style="width: 600px; height: 450px;">
															<?php echo $rs['content'];?>
														</textarea>
													</td>
												</tr>
												<tr>
													<td align="right">&nbsp;</td>
													<td>
														<input type="submit" class="btn" id="submitBtn" value="提交" /> 
														<input name="button" type="button" class="btn" onClick="javascript:history.back();" value="返回">
													</td>
												</tr>
											</table>
										</form>
									</td>
								</tr>
							</table>
						</td>
						<td width="1%">&nbsp;</td>
					</tr>
					<tr><td height="20"></td></tr>
				</table>
			</td>
		</tr>
	</table>
</body>
</html>